<template>
  <Container class="public-list">
    <HeadFormItem
      slot="header"
      :list="list"
      @onSearchBtnClick="onSearchBtnClick"
    />
    <Tabs v-model="tab">
      <div slot="extra">
        <Button size="large" class="item" type="primary" @click="setRules">公共资源库设置</Button>
        <export-btn 
          :param="exportParam" 
          url="/store_public_resource/export"
          class="item" 
          slot="extra"></export-btn>
      </div>
      <TabPane label="公共资源库列表" name="tab1">
        <homeTable
          :list="list"
          @getData="getData"
          @onPageSizeChange="onPageSizeChange"
          @onPageChange="onPageChange"
          @onSelectionChange="onSelectionChange"
        />
      </TabPane>
    </Tabs>
    <ListModelItem ref="listModelItem" />
  </Container>
</template>
<script>
import StoreListData from './list/StoreListData'
import HeadFormItem from './list/HeadFormItem'
import ListModelItem from './list/ListModelItem'
import SelectSearch from '@/components/SelectSearch'
import homeTable from '@/components/homeTable'

import { clearEmpty } from '@/util'
export default {
  name: 'manage/public/list',
  components: {
    HeadFormItem,
    SelectSearch,
    homeTable,
    ListModelItem
  },
  data() {
    return {
      tab: "tab1",
      list: StoreListData,
      productIds: []
    };
  },
  mounted() {
    this.getData();
    this.$bus.$on('detailRow', this.signInfo)
    this.$bus.$on('editRow', this.signEdit)
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    setRules() {
      this.$refs.listModelItem.openModal();
    },
    onSearchBtnClick() {
      this.list.currentPage = 1;
      this.getData();
    },
    signEdit(row) {
      this.$router.push({
        name: 'manage/public/edit',
        query: {
          id: row.id
        }
      })
    },
    signInfo(row) {
      this.$router.push({
        name: 'manage/public/edit',
        query: {
          id: row.id
        }
      })
    },
    getData() {
      this.productIds = [];
      this.list.status = "loading";
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        ...this.list.form,
        officeId: (this.list.form.officeId && Object.keys(this.list.form.officeId).length > 0) ? this.list.form.officeId.id : null,
        departmentId: this.list.form.departmentId,
      };
      param.distributorIdList = this.list.form.distributorIds
      // delete param.distributorIds
      param = clearEmpty(param)
      this.$jsonPost("/store_public_resource/page", param).then(data => {
        this.list.data = [];
        this.list.totalItem = Number(data.total);
        this.list.data = (data.records || [])
        this.list.status = "pass";
      }).catch(() => {
        this.list.status = "error";
      });
    },
    onSelectionChange(selection) {
      this.productIds = (selection || []).map(item => item.id);
    },
    onPageChange(number) {
      this.list.currentPage = number;
      this.getData();
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1;
      this.list.pageSize = pageSize;
      this.getData();
    },
    onRefresh() {
      this.getData();
    }
  },
  watch: {
  },
  computed: {
    exportParam() {
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        ...this.list.form,
        officeId: (this.list.form.officeId && Object.keys(this.list.form.officeId).length > 0) ? this.list.form.officeId.id : null,
        depId: this.list.form.departmentId,
      };
      param.distributorIdList = this.list.form.distributorIds
      // delete param.distributorIds
      param = clearEmpty(param)
      return param
    }
  }
};
</script>
